package jay.jps;


import java.util.Date;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;



public class MyGPSActivity extends Activity {
	Button btnStopService;    
	double lat,lon;
	TextView txtMsg;
	Intent  intentMyService;
	ComponentName service;
	BroadcastReceiver receiver;
	String GPS_FILTER = "guc.action.GPS_LOCATION";
	Button bando;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gps_test_activity);
		intentMyService = new Intent(this, MyGPSService.class); 
		this.startService(intentMyService);

		txtMsg = (TextView) findViewById(R.id.txtMsg);
		txtMsg.setText("MyGpsService started - (see DDMS Log)");

		IntentFilter mainFilter = new IntentFilter(GPS_FILTER);
		receiver = new MyMainLocalReceiver();
		this.registerReceiver(receiver, mainFilter); 
		btnStopService = (Button) findViewById(R.id.btnStopService);
		//		Log.e("pro", txtMsg.getText().toString());
	}
	public void Bando(View v)
	{
		//bando=(Button)findViewById(R.id.bd);
		String geoCode= " geo:"+lat+","+lon; 
		Intent intent= new Intent(Intent.ACTION_VIEW, Uri.parse(geoCode));
		
		startActivity(intent);

	}
	public void onClickBtn(View view){
		try {
			stopService(intentMyService);
			btnStopService.setText("stoped");
			btnStopService.setClickable(false);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("abc", "ccc");
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		try {
			stopService(intentMyService);
			unregisterReceiver(receiver);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	private class MyMainLocalReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			//Toast.makeText(getApplicationContext(), "da nhan lai tren activity", Toast.LENGTH_LONG).show();
			Date dt= new Date();
			int gio = dt.getHours();
			int phut=dt.getMinutes();
			int giay=dt.getSeconds();
			lat = intent.getDoubleExtra("lat", -1);
			lon = intent.getDoubleExtra("lon", -1);
			Log.e("Main>>>", Double.toString(lat));
			Log.e("Main>>>", Double.toString(lon));
			String msg = Double.toString(lat) +":" + Double.toString(lon) + ":" + String.valueOf(gio) + " :"+ String.valueOf(phut);
			txtMsg.append("\n" + msg);
			//texting(msg);
			//sendSMS("tel:01272893929", msg);
		}

	}
}
